import user from 'api/user.js'
import { arrayCopy, objectCopy } from 'utils'

export default {
	namespaced: true,
	state: {
		count: {
			done: 11,
			error: 6,
			remain: 3328,
			total: 3339,
		},
		courseSelection: 1,
		hisScores: [{
			course: 2,
			createtime: "2016-12-07 12:05:17",
			id: 5,
			score: 1,
			userid: 1,
		}],
	},
	mutations: {
		count({ count }, newCount){
			objectCopy(count, newCount);
		},
		hisScores({ hisScores }, newScores){
			arrayCopy(hisScores, newScores);
		},
		courseSelection(state, value) {
			state.courseSelection = value;
		}
	},
	actions: {
		getHisScores({ commit, actions, rootState, rootGetters }, courseKey = 1) {
			let sessionid = rootState.user.sessionid;
			let logged = rootGetters['user/logged'];

			if (logged) {
				return user.getHisScores(sessionid, courseKey)
							.done(data => {
								commit('count', data.count)
								commit('hisScores', data.his);
							});
			}

		},
		getAllHisScores({ state, commit }) {
			
		}
	}
}